var util = require("../util"), wx = "object" == typeof WXEnvironment && WXEnvironment.weexVersion ? WXEnvironment : undefined, win = "object" == typeof window && window.addEventListener ? window : undefined, isH5 = !wx || "Web" === wx.platform, cookieCache = {}, cookie = function(e, i, t, o, n) {
    var r = win && window.document;
    if (r) {
        if (i === undefined) {
            var u, c;
            if (!cookieCache[e]) {
                u = new RegExp(e + "=([^;]+)");
                try {
                    c = u.exec(r.cookie);
                } catch (d) {
                    return util.warn("[retcode] can not get cookie:", d), null;
                }
                c && (cookieCache[e] = c[1]);
            }
            return cookieCache[e];
        }
        var a = e + "=" + i;
        o && (a += "; domain=" + o), a += "; path=" + (n || "/"), t && (a += "; max-age=" + t);
        try {
            return r.cookie = a, !!r.cookie;
        } catch (d) {
            return util.warn("[retcode] can not set cookie: ", d), !1;
        }
    }
}, weexCookie = function(e, i, t, o) {
    try {
        if ("object" != typeof e || !e) return;
        if (t === undefined && "function" == typeof e.getAllObjects) {
            for (var n = e.getAllObjects() || [], r = null, u = 0; u < n.length; u++) if (n[u].name === i) {
                r = n[u];
                break;
            }
            return r && r.value;
        }
        if ("function" == typeof e.set) {
            var c = i + "=" + t + ";max-age=" + o;
            e.set(c);
        }
    } catch (a) {
        util.warn("[arms] weexCookie fail", a);
    }
};

module.exports = {
    wx: wx,
    win: win,
    isH5: isH5,
    getSr: function() {
        return isH5 ? screen.width + "x" + screen.height : wx.deviceWidth + "x" + wx.deviceHeight;
    },
    getWeexUid: function(e) {
        var i = e._conf.uid || "";
        if (i) return i;
        var t = "function" == typeof __weex_require__ && __weex_require__("@weex-module/cookie"), o = "object" == typeof weex && weex && weex.requireModule("cookie") || t;
        if (!i && "object" == typeof o && o && ((i = weexCookie(o, "_bl_uid")) || (i = util.uu(), 
        weexCookie(o, "_bl_uid", i, 15552e3))), !i && isH5 && win && win.document && !(i = cookie("_bl_uid"))) {
            i = util.uu();
            if (!cookie("_bl_uid", i, 15552e3)) return null;
        }
        return i;
    },
    getH5Page: function(e, i) {
        var t = location, o = t.host + t.pathname;
        return util.filterByRule(i ? o.toLowerCase() : o, e);
    },
    getPvInfo: function() {
        var e = isH5 ? document : undefined, i = isH5 ? win.devicePixelRatio || 1 : 1;
        return isH5 ? {
            begin: Date.now(),
            dt: e.title,
            dl: location.href,
            dr: e.referrer,
            dpr: i.toFixed(2),
            de: (e.characterSet || e.defaultCharset || "").toLowerCase(),
            ul: e.documentElement.lang
        } : {
            begin: Date.now()
        };
    }
};